Skip to content

Conversation

@nigrosimone
Copy link
Contributor

@nigrosimone nigrosimone commented Jan 17, 2026

This PR improves performance when serializing small arrays and small objects

see short array and objects

image
Checking out "perf-2"
Execute "npm run bench"

> [email protected] bench
> node --expose-gc ./benchmark/bench.js

short string............................................. x 10,314,779 ops/sec ±2.04% (11804173 runs sampled)
unsafe short string...................................... x 14,868,955 ops/sec ±0.25% (19540838 runs sampled)
short string with double quote........................... x 10,226,795 ops/sec ±0.23% (11648155 runs sampled)
long string without double quotes........................ x 45,958 ops/sec ±0.42% (43701 runs sampled)
unsafe long string without double quotes................. x 15,174,516 ops/sec ±0.24% (19908809 runs sampled)
long string.............................................. x 42,586 ops/sec ±0.43% (40122 runs sampled)
unsafe long string....................................... x 16,021,631 ops/sec ±0.25% (20963143 runs sampled)
number................................................... x 15,667,861 ops/sec ±0.24% (20543235 runs sampled)
integer.................................................. x 15,881,695 ops/sec ±0.20% (20874133 runs sampled)
formatted date-time...................................... x 1,218,848 ops/sec ±0.30% (1143062 runs sampled)
formatted date........................................... x 797,356 ops/sec ±0.70% (764895 runs sampled)
formatted time........................................... x 942,976 ops/sec ±0.30% (894909 runs sampled)
short array of numbers................................... x 85,795 ops/sec ±0.45% (81076 runs sampled)
short array of integers.................................. x 83,377 ops/sec ±0.45% (78114 runs sampled)
short array of short strings............................. x 21,193 ops/sec ±0.46% (20252 runs sampled)
short array of long strings.............................. x 20,860 ops/sec ±0.53% (19834 runs sampled)
short array of objects with properties of different types x 10,881 ops/sec ±0.64% (10384 runs sampled)
object with number property.............................. x 14,370,837 ops/sec ±0.23% (18779985 runs sampled)
object with integer property............................. x 15,136,422 ops/sec ±0.26% (19788987 runs sampled)
object with short string property........................ x 11,372,900 ops/sec ±1.64% (14210927 runs sampled)
object with long string property......................... x 42,709 ops/sec ±0.39% (40662 runs sampled)
object with properties of different types................ x 2,021,814 ops/sec ±0.21% (1916219 runs sampled)
simple object............................................ x 8,373,844 ops/sec ±0.96% (7148707 runs sampled)
simple object with required fields....................... x 8,419,553 ops/sec ±1.00% (7145667 runs sampled)
object with const string property........................ x 14,698,883 ops/sec ±0.27% (19288948 runs sampled)
object with const number property........................ x 14,407,711 ops/sec ±0.29% (18832321 runs sampled)
object with const bool property.......................... x 14,217,914 ops/sec ±0.22% (18591299 runs sampled)
object with const object property........................ x 15,132,398 ops/sec ±0.25% (19832920 runs sampled)
object with const null property.......................... x 15,699,999 ops/sec ±0.22% (20601252 runs sampled)

Checking out "main"
Execute "npm run bench"

> [email protected] bench
> node --expose-gc ./benchmark/bench.js

short string............................................. x 11,270,685 ops/sec ±2.10% (14048918 runs sampled)
unsafe short string...................................... x 13,916,374 ops/sec ±0.21% (18286851 runs sampled)
short string with double quote........................... x 10,158,859 ops/sec ±0.25% (11262922 runs sampled)
long string without double quotes........................ x 49,121 ops/sec ±0.41% (46133 runs sampled)
unsafe long string without double quotes................. x 15,779,105 ops/sec ±0.23% (20711344 runs sampled)
long string.............................................. x 43,087 ops/sec ±0.41% (40807 runs sampled)
unsafe long string....................................... x 14,168,848 ops/sec ±0.19% (18635923 runs sampled)
number................................................... x 14,642,218 ops/sec ±0.26% (19174971 runs sampled)
integer.................................................. x 15,884,010 ops/sec ±0.26% (20806675 runs sampled)
formatted date-time...................................... x 1,221,368 ops/sec ±0.26% (1156513 runs sampled)
formatted date........................................... x 937,304 ops/sec ±0.59% (889460 runs sampled)
formatted time........................................... x 941,053 ops/sec ±0.31% (878951 runs sampled)
short array of numbers................................... x 85,600 ops/sec ±0.42% (81031 runs sampled)
short array of integers.................................. x 81,975 ops/sec ±0.43% (77194 runs sampled)
short array of short strings............................. x 20,711 ops/sec ±0.56% (19579 runs sampled)
short array of long strings.............................. x 18,500 ops/sec ±0.55% (17628 runs sampled)
short array of objects with properties of different types x 10,258 ops/sec ±0.61% (9876 runs sampled)
object with number property.............................. x 13,576,798 ops/sec ±0.15% (17836312 runs sampled)
object with integer property............................. x 14,186,748 ops/sec ±0.16% (18690860 runs sampled)
object with short string property........................ x 8,333,125 ops/sec ±0.40% (7800321 runs sampled)
object with long string property......................... x 29,217 ops/sec ±0.63% (26482 runs sampled)
object with properties of different types................ x 1,501,540 ops/sec ±0.96% (1278483 runs sampled)
simple object............................................ x 8,058,341 ops/sec ±0.83% (6638208 runs sampled)
simple object with required fields....................... x 7,652,184 ops/sec ±0.97% (6011514 runs sampled)
object with const string property........................ x 14,924,645 ops/sec ±0.21% (19610912 runs sampled)
object with const number property........................ x 14,692,297 ops/sec ±0.19% (19299442 runs sampled)
object with const bool property.......................... x 14,360,540 ops/sec ±0.20% (18902627 runs sampled)
object with const object property........................ x 14,269,059 ops/sec ±0.21% (18680388 runs sampled)
object with const null property.......................... x 16,383,306 ops/sec ±0.26% (21411387 runs sampled)

short string..............................................-8.48%
unsafe short string.......................................+6.85%
short string with double quote............................+0.67%
long string without double quotes.........................-6.44%
unsafe long string without double quotes..................-3.83%
long string...............................................-1.16%
unsafe long string.......................................+13.08%
number.......................................................+7%
integer...................................................-0.01%
formatted date-time.......................................-0.21%
formatted date...........................................-14.93%
formatted time.............................................+0.2%
short array of numbers....................................+0.23%
short array of integers...................................+1.71%
short array of short strings..............................+2.33%
short array of long strings..............................+12.76%
short array of objects with properties of different types.+6.07%
object with number property...............................+5.85%
object with integer property..............................+6.69%
object with short string property........................+36.48%
object with long string property.........................+46.18%
object with properties of different types................+34.65%
simple object.............................................+3.92%
simple object with required fields.......................+10.03%
object with const string property.........................-1.51%
object with const number property.........................-1.94%
object with const bool property...........................-0.99%
object with const object property.........................+6.05%
object with const null property...........................-4.17%
Back to perf-2 4c07947
  • Iteration changed from Object.entries(obj) to Object.keys(obj) and uses obj[key] when needed.
  • Replaces runtime propertiesKeys.includes(key) (O(n) per check) with an inline sequence of equality checks when properties exist (key === 'a' || key === 'b' ...). This inlines known property checks into generated code.
  • Moves retrieval of value after the continue checks to avoid unnecessary reads/allocation for skipped keys.
  • Keeps checks to skip undefined, function, and symbol values.
  • New fast-path when requiredProperties.length > 0
  • Avoids per-iteration branch for commas by special-casing the first element (serialize index 0 without a comma)

@nigrosimone nigrosimone changed the title perf: breaks the JSON.stringify advantage and the sound barrier perf: eliminates JSON.stringify’s advantage and breaks the sound barrier Jan 17, 2026
@nigrosimone nigrosimone changed the title perf: eliminates JSON.stringify’s advantage and breaks the sound barrier perf: Array and object Jan 18, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant